<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/2/11
 * Time: 1:17
 */

use yii\helpers\Url;

$web = Url::base();
?>


<div class="form-horizontal" id="data_form">
    <iframe id="upload_image_target" name="upload_image_target" style="display: none;"></iframe>


    <input type="hidden" id="id" name="id" class="form-control"
           value="<?= isset($item['id']) != null ? $item['id'] : '' ?>">

    <div class="form-group">
        <label class="control-label col-lg-2" for="name"><span
                    class="font-color-attention">*</span>名称：</label>
        <div class="col-lg-10">
            <input type="text" id="name" name="name" class="form-control"
                   value="<?= $item != null ? $item['name'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="summary"><span
                    class="font-color-attention">*</span>简介：</label>
        <div class="col-lg-10">
            <input type="text" id="summary" name="summary" maxlength="30" class="form-control"
                   value="<?= $item != null ? $item['summary'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="unit"><span
                    class="font-color-attention">*</span>单位：</label>
        <div class="col-lg-10">
            <input type="text" id="unit" name="unit" class="form-control"
                   value="<?= $item != null ? $item['unit'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="stock"><span
                    class="font-color-attention">*</span>库存数：</label>
        <div class="col-lg-10">
            <input type="number" id="stock" name="stock" class="form-control"
                   value="<?= $item != null ? $item['stock'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="settlement_price"><span
                    class="font-color-attention">*</span>成本价：</label>
        <div class="col-lg-10">
            <input type="number" id="settlement_price" name="settlement_price" class="form-control"
                   value="<?= $item != null ? $item['settlement_price'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="selling_price"><span
                    class="font-color-attention">*</span>最终销售价：</label>
        <div class="col-lg-10">
            <input type="number" id="selling_price" name="selling_price" class="form-control"
                   value="<?= $item != null ? $item['selling_price'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="old_price"><span
                    class="font-color-attention">*</span>原价：</label>
        <div class="col-lg-10">
            <input type="number" id="old_price" name="old_price" class="form-control"
                   value="<?= $item != null ? $item['old_price'] : '' ?>">
        </div>
    </div>

    <!--上传图片开始-->
    <form id="small_upload_image_submit_form" enctype="multipart/form-data" method="post"
          action=<?= \app\librarys\Tools::urlBase() . '/main/upload-image-handler' ?> target="upload_image_target">
        <div class="form-group">
            <label class="control-label col-lg-2">上传小图:</label>
            <div class="product-mod col-lg-10">
                <div class="upload-img">
                    <div class="uploadImg">
                        <input class="upload-img-box" type="file" onchange="uploadSmallImage(this)"
                               name="uploadImg" id="uploadImg">
                        <input type="hidden" name="img_cord" id="img_cord"/>
                    </div>
                    <a href="javascript:void(0)" class="btn btn-warning">选择图片(jpeg,png,jpg)</a>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-2"></label>
            <div class="product-mod col-lg-10">

                <div id="imgSingle_Sure">
                    <div class="pre_imgs">
                        <!--                    --><? //=$item['small_image_url']?>
                        <?php if (!empty($item['small_image_url'])): ?>
                            <img src="<?= \app\models\File::getFile($item['small_image_url'])['url'] ?>"
                                 style="width: 200px;"
                                 onerror="showErrorImage(this)">
                        <?php endif ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <!--上传图片结束-->


    <!--上传产品介绍图开始-->
    <form id="pro_upload_image_submit_form" enctype="multipart/form-data" method="post"
          action=<?= \app\librarys\Tools::urlBase() . '/main/upload-image-handler' ?> target="upload_image_target">
        <div class="form-group">
            <label class="control-label col-lg-2">上传产品介绍图:</label>
            <div class="product-mod col-lg-10">
                <div class="upload-img">
                    <div class="uploadImg">
                        <input class="upload-img-box" type="file" onchange="uploadProImage(this)"
                               name="uploadImg" id="uploadImg2">
                        <input type="hidden" name="img_cord" id="img_cord2"/>
                    </div>
                    <a href="javascript:void(0)" class="btn btn-warning">选择图片(jpeg,png,jpg)</a>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-2"></label>
            <div class="product-mod col-lg-10">

                <div id="imgSingle_Sure" class="pre_imgs">
                    <?php
                    if ($pro_images) {
                        foreach ($pro_images as $pro_image) {
                            ?>
                            <div img-id="<?= $pro_image['file_id'] ?>"><img
                                        src="<?= \app\models\File::getFile($pro_image['file_id'])['url'] ?>"/>
                                <div onclick='delProImg(this)' class='del_icon'
                                     style='background:url(<?= $web . '/images/del-icon-hover.png' ?>);width:26px;height:26px;position: absolute;right: 0px;top:10px;z-index: 999;cursor:pointer;'></div>
                            </div>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </form>
    <!--上传产品介绍图结束-->


    <!--上传产品详情图开始-->
    <form id="detail_upload_image_submit_form" enctype="multipart/form-data" method="post"
          action=<?= \app\librarys\Tools::urlBase() . '/main/upload-image-handler' ?> target="upload_image_target">
        <div class="form-group">
            <label class="control-label col-lg-2">上传产品详情图:</label>
            <div class="product-mod col-lg-10">
                <div class="upload-img">
                    <div class="uploadImg">
                        <input class="upload-img-box" type="file" onchange="uploadDetailImage(this)"
                               name="uploadImg" id="uploadImg3">
                        <input type="hidden" name="img_cord" id="img_cord3"/>
                    </div>
                    <a href="javascript:void(0)" class="btn btn-warning">选择图片(jpeg,png,jpg)</a>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-2"></label>
            <div class="product-mod col-lg-10">

                <div id="imgSingle_Sure" class="pre_imgs">
                    <?php
                    if ($detail_images) {
                        foreach ($detail_images as $detail_image) {
                            ?>
                            <div img-id="<?= $detail_image['file_id'] ?>"><img
                                        src="<?= \app\models\File::getFile($detail_image['file_id'])['url'] ?>"/>
                                <div onclick='delProImg(this)' class='del_icon'
                                     style='background:url(<?= $web . '/images/del-icon-hover.png' ?>);width:26px;height:26px;position: absolute;right: 0px;top:10px;z-index: 999;cursor:pointer;'></div>
                            </div>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </form>
    <!--上传产品详情图结束-->


    <div class="form-group">
        <label class="control-label col-lg-2" for="is_enabled"><span
                    class="font-color-attention">*</span>是否启用：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_enabled" name="is_enabled">
                <option value="1" <?php echo $item['is_enabled'] == '1' ? 'selected' : '' ?>>启用</option>
                <option value="0" <?php echo $item['is_enabled'] == '0' ? 'selected' : '' ?>>禁用</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_shelved"><span
                    class="font-color-attention">*</span>是否上架：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_shelved" name="is_shelved">
                <option value="1" <?php echo $item['is_shelved'] == '1' ? 'selected' : '' ?>>上架</option>
                <option value="0" <?php echo $item['is_shelved'] == '0' ? 'selected' : '' ?>>下架</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_recommend"><span
                    class="font-color-attention">*</span>是否推荐：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_recommend" name="is_recommend">
                <option value="1" <?php echo $item['is_recommend'] == '1' ? 'selected' : '' ?>>推荐</option>
                <option value="0" <?php echo $item['is_recommend'] == '0' ? 'selected' : '' ?>>未推荐</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_newest"><span
                    class="font-color-attention">*</span>是否新品：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_newest" name="is_newest">
                <option value="1" <?php echo $item['is_newest'] == '1' ? 'selected' : '' ?>>新品</option>
                <option value="0" <?php echo $item['is_newest'] == '0' ? 'selected' : '' ?>>非新品</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_hot"><span
                    class="font-color-attention">*</span>是否热销：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_hot" name="is_hot">
                <option value="1" <?php echo $item['is_hot'] == '1' ? 'selected' : '' ?>>热销</option>
                <option value="0" <?php echo $item['is_hot'] == '0' ? 'selected' : '' ?>>非热销</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_free_express"><span
                    class="font-color-attention">*</span>是否包邮：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_free_express" name="is_free_express">
                <option value="1" <?php echo $item['is_free_express'] == '1' ? 'selected' : '' ?>>包邮</option>
                <option value="0" <?php echo $item['is_free_express'] == '0' ? 'selected' : '' ?>>不包邮</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_promotion"><span
                    class="font-color-attention">*</span>是否促销：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_promotion" name="is_promotion">
                <option value="1" <?php echo $item['is_promotion'] == '1' ? 'selected' : '' ?>>促销</option>
                <option value="0" <?php echo $item['is_promotion'] == '0' ? 'selected' : '' ?>>非促销</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="sell_count"><span
                    class="font-color-attention">*</span>虚拟销售量：</label>
        <div class="col-lg-10">
            <input type="number" id="sell_count" name="sell_count" class="form-control"
                   value="<?= $item != null ? $item['sell_count'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="collected_count"><span
                    class="font-color-attention">*</span>虚拟被收藏数量：</label>
        <div class="col-lg-10">
            <input type="number" id="collected_count" name="collected_count" class="form-control"
                   value="<?= $item != null ? $item['collected_count'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="express_money"><span
                    class="font-color-attention">*</span>邮费：</label>
        <div class="col-lg-10">
            <input type="number" id="express_money" name="express_money" class="form-control"
                   value="<?= $item != null ? $item['express_money'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="sort"><span
                    class="font-color-attention">*</span>排序（数字越大越靠前）：</label>
        <div class="col-lg-10">
            <input type="number" id="sort" name="sort" class="form-control"
                   value="<?= $item != null ? $item['sort'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_invoice"><span
                    class="font-color-attention">*</span>是否提供发票：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_invoice" name="is_invoice">
                <option value="1" <?php echo $item['is_invoice'] == '1' ? 'selected' : '' ?>>提供发票</option>
                <option value="0" <?php echo $item['is_invoice'] == '0' ? 'selected' : '' ?>>无发票（不显示）</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_GGCT"><span
                    class="font-color-attention">*</span>是否新品：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_GGCT" name="is_GGCT">
                <option value="1" <?php echo $item['is_GGCT'] == '1' ? 'selected' : '' ?>>正品保证</option>
                <option value="0" <?php echo $item['is_GGCT'] == '0' ? 'selected' : '' ?>>无保证（不显示）</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_seven_day"><span
                    class="font-color-attention">*</span>是否支持7天无理由退款：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_seven_day" name="is_seven_day">
                <option value="1" <?php echo $item['is_seven_day'] == '1' ? 'selected' : '' ?>>支持</option>
                <option value="0" <?php echo $item['is_seven_day'] == '0' ? 'selected' : '' ?>>不支持（不显示）</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_guarantee"><span
                    class="font-color-attention">*</span>是否保修：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_guarantee" name="is_guarantee">
                <option value="1" <?php echo $item['is_guarantee'] == '1' ? 'selected' : '' ?>>保修</option>
                <option value="0" <?php echo $item['is_guarantee'] == '0' ? 'selected' : '' ?>>不保修（不显示）</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_return_goods"><span
                    class="font-color-attention">*</span>是否支持退货：</label>
        <div class="col-lg-4">
            <select class='select-group' id="is_return_goods" name="is_return_goods">
                <option value="1" <?php echo $item['is_return_goods'] == '1' ? 'selected' : '' ?>>退货</option>
                <option value="0" <?php echo $item['is_return_goods'] == '0' ? 'selected' : '' ?>>不退货（不显示）</option>
            </select>
        </div>
    </div>
</div>


<script type="text/javascript">
    $('.form_date').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });
</script>

<script type="text/javascript" src="<?= $web ?>/js/jcrop/jcrop.single.js"></script>
<script type="text/javascript">
    function uploadSmallImage(obj) {
        previewSingleChange(obj, 1, 1, getSmallImgPathFun);
    }

    function getSmallImgPathFun() {
        $imgCord = $("input[name='imgCord']").val();
        $("#small_upload_image_submit_form").find("#img_cord").val($imgCord);

        $("#upload_image_target").html('');
        $("#small_upload_image_submit_form").submit();
        $("#upload_image_target").unbind().load(function () {
            try {
                var result = JSON.parse($("#upload_image_target").contents().find("body").html());
                if (result.status == 0) {
                    var path = '<?= $web?>' + result.data.path;
                    if (result.data.path) {
                        setSmallImgPath(result.data.path);
                    }

                    var html = '<img src="' + path + '" style="width: 200px; margin-left: 10px; margin-top: 10px">';
                    $("#small_upload_image_submit_form").find("#imgSingle_Sure").html('').append(html);
                    closeCutImage();
                } else {
                    errorDialog(result.message);
                }
            } catch (err) {
                errorDialog("保存出错");
            }
        });
    }


    function uploadProImage(obj) {
        previewSingleChange(obj, 4, 3, getProImgPathFun);
    }

    function getProImgPathFun() {
        $imgCord = $("input[name='imgCord']").val();
        $("#pro_upload_image_submit_form").find("#img_cord2").val($imgCord);

        $("#upload_image_target").html('');
        $("#pro_upload_image_submit_form").submit();
        $("#upload_image_target").unbind().load(function () {
            debugger;
            try {
                var result = JSON.parse($("#upload_image_target").contents().find("body").html());
                if (result.status == 0) {
                    var path = '<?= $web?>' + result.data.path;
                    if (result.data.path) {
                        setProImgPath(result.data.path);
                    }

                    var html = '<div path="' + path + '"><img src="' + path + '" >';
                    html += "<div onclick='delProImg(this)' class='del_icon' style='background:url(<?=$web . '/images/del-icon-hover.png'?>);width:26px;height:26px;position: absolute;right: 0px;top:10px;z-index: 999;cursor:pointer;'></div>";
                    html += '</div>';


                    $("#pro_upload_image_submit_form").find("#imgSingle_Sure").append(html);
                    closeCutImage();
                } else {
                    errorDialog(result.message);
                }
            } catch (err) {
                errorDialog("保存出错");
            }
        });
    }

    function delProImg(obj) {
        var path = $(obj).parent().attr("path");
        if (path) {
            pro_img_paths.remove(path);
            $(obj).parent().remove();
        } else {
            var id = $("#data_form").find("#id").val();
            var img_id = $(obj).parent().attr("img-id");

            var data = {};
            data.id = id;
            data.img_id = img_id;
            requestJson("goods-delete-image-handler", data, true, function (response) {
                if (response.status == 0) {
                    $(obj).parent().remove();
                } else {
                    errorDialog(response.message);
                }
            });
        }

    }


    function uploadDetailImage(obj) {
        previewSingleChange(obj, 0, 0, getDetailImgPathFun);
    }

    function getDetailImgPathFun() {
        $imgCord = $("input[name='imgCord']").val();
        $("#detail_upload_image_submit_form").find("#img_cord3").val($imgCord);

        $("#upload_image_target").html('');
        $("#detail_upload_image_submit_form").submit();
        $("#upload_image_target").unbind().load(function () {
            try {
                var result = JSON.parse($("#upload_image_target").contents().find("body").html());
                if (result.status == 0) {
                    var path = '<?= $web?>' + result.data.path;
                    if (result.data.path) {
                        setDetailImgPath(result.data.path);
                    }

                    var html = '<div path="' + path + '"><img src="' + path + '" >';
                    html += "<div onclick='delDetailImg(this)' class='del_icon' style='background:url(<?=$web . '/images/del-icon-hover.png'?>);width:26px;height:26px;position: absolute;right: 0px;top:10px;z-index: 999;cursor:pointer;'></div>";
                    html += '</div>';
                    $("#detail_upload_image_submit_form").find("#imgSingle_Sure").append(html);
                    closeCutImage();
                } else {
                    errorDialog(result.message);
                }
            } catch (err) {
                errorDialog("保存出错");
            }
        });
    }

    function delDetailImg(obj) {
        var path = $(obj).parent().attr("path");
        detail_img_paths.remove(path);
        $(obj).parent().remove();
        alert("新加的删除数组，老的直接删除");
    }
</script>
